package quizman.tdg;
/**
 * 
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.dsrg.ts.threadLocal.DbRegistry;

/**
 * @author Team 0i
 *
 */
public class MultipleChoiceTDG {
	public static ResultSet find(String questionName) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to MultipleChoiceTDG.find.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("SELECT * from Possible_Answer where Question LIKE ?");
			dbStatement.setString(1, questionName);
			ResultSet rs = dbStatement.executeQuery();
			return rs;
		} catch (Exception e) {
			String msg = "Error in MultipleChoiceTDG.find";
			throw new Exception(msg, e);
		}
	}
	
	public static void update(String questionName, String answerText, String newAnswerText, boolean isRight) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to MultipleChoiceTDG.update.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("UPDATE Possible_Answer SET Answer_Text = ? , isRight = ? WHERE Question LIKE ? AND Answer_Text LIKE ?");
			dbStatement.setString(1, newAnswerText);
			if(isRight) dbStatement.setString(2, "TRUE");
			else dbStatement.setString(2,"FALSE");
			dbStatement.setString(3, questionName);
			dbStatement.setString(4, answerText);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in MultipleChoiceTDG.update";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(String questionName, String answerText, boolean isRight) throws Exception{
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to MultipleChoiceTDG.insert.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("INSERT INTO Possible_Answer (Answer_Text, isRight, Question, Answer_Text) values(?, ?, ?, ?)");
			dbStatement.setString(1, answerText);
			if(isRight) dbStatement.setString(2, "TRUE");
			else dbStatement.setString(2,"FALSE");
			dbStatement.setString(3, questionName);
			dbStatement.setString(4, answerText);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in MultipleChoiceTDG.insert";
			throw new Exception(msg, e);
		}
	}
	
	public static void delete(String questionName, String answerText) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to MultipleChoiceTDG.delete.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("DELETE FROM Possible_Answer WHERE Question LIKE ? AND Answer_Text LIKE ?");
			dbStatement.setString(1, questionName);
			dbStatement.setString(2, answerText);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in MultipleChoiceTDG.delete";
			throw new Exception(msg, e);
		}
	}
	
	public static void deleteAll(String questionName) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to MultipleChoiceTDG.deleteAll.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("DELETE FROM Possible_Answer WHERE Question LIKE ?");
			dbStatement.setString(1, questionName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in MultipleChoiceTDG.deleteAll";
			throw new Exception(msg, e);
		}
	}
}